package com.ahnchan.ContactsPlus;

import com.ahnchan.ContactsPlus.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.Toast;

public class ContactsPlusActivity extends TabActivity {
	
	public ContactsPlusSetting setting;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.main);

    	Log.d("ContactPlus", "Start Contacts Plus");
    	
        // Get Properties
        setting = ContactsPlusSetting.instatnce();
        
        // Check setting update info
        String strLanguage = this.getResources().getConfiguration().locale.getLanguage();
        String strCountry = this.getResources().getConfiguration().locale.getCountry();
        setting.checkLanguage(strLanguage, strCountry);
        
    	Log.d("ContactPlus", "[ContactsPlusActivity] " + strLanguage + "," + 
    						this.getResources().getConfiguration().locale.getDisplayLanguage() + "," +
    						this.getResources().getConfiguration().locale.getCountry() + "," + 
    						this.getResources().getConfiguration().locale.getDisplayCountry() + ",");

    	// Setting Tab
        Resources res = getResources();
        TabHost tabHost = getTabHost();
        TabHost.TabSpec spec;
        Intent intent;
        
        // Contacts
        intent = new Intent().setClass(this, ContactsActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        spec = tabHost.newTabSpec("contact").setIndicator(res.getString(R.string.contact_tab), res.getDrawable(R.drawable.ic_tab_contact)).setContent(intent);
        tabHost.addTab(spec);
        
        // Group
        intent = new Intent().setClass(this, GroupActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        spec = tabHost.newTabSpec("group").setIndicator(res.getString(R.string.group_tab), res.getDrawable(R.drawable.ic_tab_group)).setContent(intent);
        tabHost.addTab(spec);

        // Company
        intent = new Intent().setClass(this, CompanyActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        spec = tabHost.newTabSpec("company").setIndicator(res.getString(R.string.company_tab), res.getDrawable(R.drawable.ic_tab_company)).setContent(intent);
        tabHost.addTab(spec);
        
        // Favorites
        intent = new Intent().setClass(this, FavoritesActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        spec = tabHost.newTabSpec("favorites").setIndicator(res.getString(R.string.favorites_tab), res.getDrawable(R.drawable.ic_tab_favorites)).setContent(intent);
        tabHost.addTab(spec);

        // set first tab
        tabHost.setCurrentTab(Integer.valueOf(setting.getFirstTab()));   
        
        
        // TODO
        // 1. Tab을 선택는 부분
        // 2. 순서?
        // 3. Default Tab
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    	
    	// to reset position
    	setting.setContactsPosition(0);
    	setting.setCompanyPosition(0);
    	setting.setGroupPosition(0);
    	setting.setFavoritesPosition(0);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.option_menu, menu);
    	
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	
    	switch (item.getItemId()) {
    	case R.id.option_setting :
    		Intent intent = new Intent(this, SettingActivity.class);
    		startActivity(intent);
    		return true;
    	case R.id.option_about :
    		Toast.makeText(getApplicationContext(), "About Dialog!! Coming soon~~!", Toast.LENGTH_SHORT);
    		return true;
    	}
    	return false;
    }
}